package com.zjzywl.iotSendthings.controller;

import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import com.zjzywl.iotSendthings.util.ImageTransferUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.zjzywl.iotSendthings.entity.IdentityCard;
import com.zjzywl.iotSendthings.repository.IdentityCardRepository;
import com.zjzywl.iotSendthings.util.WebResult;

/*
 * 身份证控制类
 */
@RestController
@RequestMapping(value = "/ic")
public class IdentityCardController extends BaseController {

    @Autowired
    private IdentityCardRepository repository;

    //检查uuid的唯一性
    @GetMapping(value = "/checkingUuid")
    public WebResult getPicture(@PathParam("uuid") String uuid) {
        IdentityCard ic = repository.findByUuid(uuid);
        if (ic != null) {
            return WebResult.FAIL("uuid重复");
        } else {
            return WebResult.OK("uuid没有重复");
        }
    }


    @PutMapping("/upload")
    public WebResult uploadImg(@RequestParam("picture") MultipartFile multipartFile, @RequestParam("uuid") String uuid, @RequestParam("cardname") String cardname, @RequestParam("cardno") String cardno
            , @RequestParam("address") String address) {
        if (multipartFile.isEmpty() || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return WebResult.FAIL("图片数据为空！");
        }
        String contentType = multipartFile.getContentType();
        if (!contentType.contains("")) {
            return WebResult.FAIL("文件类型为空！");
        }
        try {
            String file_name = ImageTransferUtil.saveImg(multipartFile, "ic", uuid);
            IdentityCard ic = new IdentityCard();
            ic.setUuid(uuid);
            ic.setCode(cardno);
            ic.setName(cardname.trim());
            ic.setAddress(address.trim());
            ic.setTime( new Date());
            ic.setPicturePath("ic\\" + file_name);
            repository.save(ic);
            if (StringUtils.isNotBlank(file_name)) {
                return WebResult.OK(file_name);
            }
        } catch (IOException e) {
            return WebResult.FAIL(e.getMessage());
        }
        return WebResult.FAIL("接收图片失败！");
    }


    @RequestMapping(value = "/getImg", method = RequestMethod.GET)
    public void doGetBackgroundImg(String uuid, HttpServletResponse response) throws Exception {
        IdentityCard ic = repository.findByUuid(uuid);
        String pictureFile = ic.getPicturePath();
        ImageTransferUtil.getPictureByUrl(response, pictureFile);
    }


    @PostMapping("/save")
    public WebResult saveIC(@PathParam("uuid") String uuid, @PathParam("code") String code, @PathParam("name") String name) {
        IdentityCard ic = new IdentityCard();
        ic.setUuid(uuid);
        ic.setCode(code);
        ic.setName(name.trim());
        ic.setTime(new Date());
        repository.save(ic);
        return WebResult.OK();
    }

    @GetMapping("/getdata/byuuid")
    public WebResult getIdentityCardByUUID(@PathParam("uuid") String uuid) {
        IdentityCard ic = repository.findByUuid(uuid);
        if (ic != null) {
            return WebResult.OK(ic);
        } else {
            return WebResult.FAIL("找不到该编号");
        }
    }

}
